﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class Good1 : System.Web.UI.Page
{
    private static string sqlconn = System.Configuration.ConfigurationManager.AppSettings["strConn"].ToString().Trim();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindYear();
            BindMonth();
            BindDay();

        }
    }
    protected void btnInsert_Click(object sender, EventArgs e)
    {
        string id = txtID.Text.Trim();
        string name = txtName.Text.Trim();
        string price = txtPrice.Text.Trim();
        string descn = txtDesc.Text.Trim();
        string num = txtNum.Text.Trim();
        string proid = txtProID.Text.Trim();
        string year = DropDownList1.SelectedItem.Text.Trim();
        string month = DropDownList2.SelectedItem.Text.Trim();
        string day = DropDownList3.SelectedItem.Text.Trim();
        string todate = txtTodate.Text.Trim();
        if (month.Length == 1)
        {
            month = "0" + month;
        }
        string prodate = year + month + day;
        users us = new users();
        us.name = name;
        us.id = id;
        us.price = price;
        us.descn = descn;
        us.num = num;
        us.proid = proid;
        us.prodate = prodate;
        us.todate = todate;
        int result = us.isExist(us);
        SqlConnection conn = new SqlConnection(sqlconn);
        try
        {
            conn.Open();
            string sql = "insert into MerchInfo (MerchID,MerchName,MerchPrice,MerchDescn,MerchNum,ProvideID,BeginDate,ToDate) VALUES ('" + id + "','" + name + "','" + price + "','" + descn + "','" + num + "','" + proid + "','" + prodate + "','" + todate + "')";
            SqlCommand comm = new SqlCommand(sql, conn);
            comm.ExecuteNonQuery();
            Response.Write("<script>alert(\"添加成功！\")</script>");
        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            conn.Close();
        }
       
    }
    void BindYear()
    {
        DropDownList1.Items.Clear();
        int StarYear = DateTime.Now.Year - 30;
        int EndYear = DateTime.Now.Year;
        for (int i = StarYear; i <= EndYear; i++)
        {
            DropDownList1.Items.Add(new ListItem(i.ToString()));
        }
        DropDownList1.SelectedValue = EndYear.ToString();
    }

    void BindMonth()
    {
        DropDownList2.Items.Clear();
        for (int i = 1; i <= 12; i++)
        {
            DropDownList2.Items.Add(new ListItem(i.ToString()));
        }
    }
    void BindDay()
    {
        DropDownList3.Items.Clear();
        string Year = DropDownList1.SelectedValue;
        string Month = DropDownList2.SelectedValue;
        int day = DateTime.DaysInMonth(int.Parse(Year), int.Parse(Month));
        for (int i = 1; i < day; i++)
        {
            DropDownList3.Items.Add(new ListItem(i.ToString()));
        }
    }
    protected void btnQuxiao_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Good1.aspx");
    }
}